'use strict';

/**
 * @name: 小程序本地执行
 * @description: 在本地执行小程序的代码
 * @version: 0.0.1
 * @author: xiech
 * @Date: 2020-12-04 14:07:42
 * @LastEditors: xiech
 * @LastEditTime: 2020-12-07 14:15:34
 */

// 小程序入口文件
const funs = require('./index.js');
// 本地mongodb
const mf_mongodb = require('./localExploit/connect_local_mongodb.js');
// top接口初始化
const ApiClient = require('./localExploit/lib/taobao.js').ApiClient; // 淘宝API
// 本地mysqldb
const mf_mysqldb = require('./localExploit/connect_local_mysqldb.js');

// Top接口使用appkey
const topApi = new ApiClient({
  appkey: '28370694',
  appsecret: '12342f554ecd9851da05743558f0dee8',
  url: 'http://gw.api.taobao.com/router/rest',
});

// 参数
const ctx = {
  'platform':'taobao',
  'env': 'node',
  'mixNick': 'z01IE98JVR+EUTRXq0dG0i0pTix+Oonf5VD0dnSUq8vAJo=',
  'data': {
    'id': '1',
  },
  'cloud':{}
};

// main 函数
(async () => {
  let mfmongodb,mfmysqldb;
  try {
    // 增加mongodb
    mfmongodb = await mf_mongodb();
    ctx.cloud.db = mfmongodb.mongodb;
    // 增加mysqldb
    mfmysqldb = await mf_mysqldb();
    ctx.cloud.dataspace = mfmysqldb;
    // 增加top接口
    ctx.cloud.topApi = topApi;
    ctx.topSession = '50000001524bkjkdWvBRTgBFCpfNWyEKlyna11f01f46Gx1HhZHvQbRvaZC0GWRtDLc';
    // 执行函数
    const data = await funs.main(ctx);
    console.log(data);
  } catch (err) {
    console.log(err);
  }finally{
    mfmongodb.db.close();
    mfmysqldb.end();
  }
})();